import {
	baseUrl
} from '../config.js'
export const fileImg = () => {
	console.log('dd');
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: 1,
			success: function(res) {
				const tempFilePaths = res.tempFilePaths; //拿到选择的图片，是一个数组
				uni.uploadFile({
					url: `${baseUrl}/api/common/uploadImg`, //post请求的地址
					filePath: tempFilePaths[0],
					name: 'images',
					header: {
						'Authorization': uni.getStorageSync('token') || ''
					},
					success: (uploadFileRes) => {
						console.log(uploadFileRes);
						try {
							const obj = JSON.parse(uploadFileRes.data);
							if (obj.code == 1) {
								resolve(obj);
							} else {
								reject(new Error(obj.msg || '上传失败'));
							}
						} catch (error) {
							reject(new Error('解析上传结果失败'));
						}
					},
					fail: (error) => {
						reject(new Error('上传失败'));
					}
				})
			}
		});
	})
}
